package com.yc.jpyy.meetcheck.controller;


import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONArray;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.common.utils.manydatasource.HandlerDataSource;
import com.yc.jpyy.base.controller.BaseController;
import com.yc.jpyy.meetcheck.service.MeetCheckService;
import com.yc.jpyy.utils.Tools;

@Controller
@RequestMapping(value = "/meetcheck")
@Scope("prototype")
public class MeetCheckController extends BaseController{
	private static Logger logger = LoggerFactory.getLogger(MeetCheckController.class);

	@Autowired
	private MeetCheckService meetCheckService;
	
	
	/**
	 * 新增
	 * 
	 * @param request
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/addMeetCheck", method = { RequestMethod.GET,
			RequestMethod.POST }, produces = "application/json; charset=utf-8")
	public @ResponseBody String addWeekCheck(HttpServletRequest request) throws UnsupportedEncodingException {
		request.setCharacterEncoding("UTF-8");
		Map<String,Object> resultMap = new HashMap<>();
		try {
			String info = request.getParameter("info");
			Map<String,Object> paramsMap = Tools.JsonToMap(info);
			String dbcode = paramsMap.get("dbcode").toString();
			HandlerDataSource.setDataSource(dbcode);
			resultMap = meetCheckService.addMeetCheck(paramsMap);
			resultMap.put("pubKey",Optional.ofNullable(request.getAttribute("pubKey")).orElse(""));
		} catch (Exception e) {
			logger.info("addWeekCheck" + e.toString());
			resultMap.put("returnCode", 0);
			resultMap.put("returnMsg", "添加失败");
		} finally {
			HandlerDataSource.clear();
			return JSON.toJSONString(resultMap);
		}
		
	}
	
	/**
	 * 批量新增
	 * 
	 * @param request
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/addBatchMeetCheck", method = { RequestMethod.GET,
			RequestMethod.POST }, produces = "application/json; charset=utf-8")
	public @ResponseBody String addBatchMeetCheck(HttpServletRequest request) throws UnsupportedEncodingException {
		request.setCharacterEncoding("UTF-8");
		Map<String,Object> resultMap = new HashMap<>();
		try {
			String info = request.getParameter("info");
			List<Map<String,Object>> dataList = new ArrayList<Map<String,Object>>();
			//将jsonArray字符串转化为JSONArray数组 
		    JSONArray jsonArray = JSONArray.fromObject(info); 
		    int jsonArraySize = jsonArray.size();//获取数组长度
			for(int i = 0; i < jsonArraySize; i++ ){
				//JSON对象转JSON字符串
				String str = jsonArray.getJSONObject(i).toString();
				Map<String,Object> paramsMap = Tools.JsonToMap(str);
				dataList.add(paramsMap);
			}
			String dbcode = dataList.get(0).get("dbcode").toString();
			HandlerDataSource.setDataSource(dbcode);
			resultMap = meetCheckService.addBatchMeetCheck(dataList);
			resultMap.put("pubKey",Optional.ofNullable(request.getAttribute("pubKey")).orElse(""));
		} catch (Exception e) {
			logger.info("addBatchMeetCheck" + e.toString());
			resultMap.put("returnCode", 0);
			resultMap.put("returnMsg", "批量添加失败");
		} finally {
			HandlerDataSource.clear();
			return JSON.toJSONString(resultMap);
		}
		
	}
	
	/**
	 * 删除
	 * 
	 * @param request
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/delMeetCheck", method = { RequestMethod.GET,
			RequestMethod.POST }, produces = "application/json; charset=utf-8")
	public @ResponseBody String delMeetCheck(HttpServletRequest request) throws UnsupportedEncodingException {
		request.setCharacterEncoding("UTF-8");
		Map<String,Object> resultMap = new HashMap<>();
		try {
			String info = request.getParameter("info");
			Map<String,Object> paramsMap = Tools.JsonToMap(info);
			String dbcode = paramsMap.get("dbcode").toString();
			HandlerDataSource.setDataSource(dbcode);
			resultMap = meetCheckService.delMeetCheck(paramsMap);
			resultMap.put("pubKey",Optional.ofNullable(request.getAttribute("pubKey")).orElse(""));
		} catch (Exception e) {
			logger.info("delMeetCheck" + e.toString());
			resultMap.put("returnCode", 0);
			resultMap.put("returnMsg", "删除失败");
		} finally {
			HandlerDataSource.clear();
			return JSON.toJSONString(resultMap);
		}
		
	}
	
	
	/**
	 * 根据ID查询
	 * 
	 * @param request
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/selectMeetCheckById", method = { RequestMethod.GET,
			RequestMethod.POST }, produces = "application/json; charset=utf-8")
	public @ResponseBody String selectMeetCheckById(HttpServletRequest request) throws UnsupportedEncodingException {
		request.setCharacterEncoding("UTF-8");
		Map<String,Object> resultMap = new HashMap<>();
		try {
			String info= request.getParameter("info");
			Map<String,Object> paramsMap = Tools.JsonToMap(info);
			String dbcode = paramsMap.get("dbcode").toString();
			HandlerDataSource.setDataSource(dbcode);
			resultMap = meetCheckService.selectMeetCheckById(paramsMap);
			resultMap.put("pubKey",Optional.ofNullable(request.getAttribute("pubKey")).orElse(""));
		} catch (Exception e) {
			logger.info("selectMeetCheckById" + e.toString());
			resultMap.put("returnCode", 0);
			resultMap.put("returnMsg", "查询失败");
		} finally {
			HandlerDataSource.clear();
			return JSON.toJSONString(resultMap);
		}
		
	}
	
	/**
	 * 分页查询主表
	 * 
	 * @param request
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/selectMeetCheckByPage", method = { RequestMethod.GET,
			RequestMethod.POST }, produces = "application/json; charset=utf-8")
	public @ResponseBody String selectMeetCheckByPage(HttpServletRequest request) throws UnsupportedEncodingException {
		request.setCharacterEncoding("UTF-8");
		Map<String,Object> resultMap = new HashMap<>();
		try {
			String info= request.getParameter("info");
			Map<String,Object> paramsMap = Tools.JsonToMap(info);
			String dbcode = paramsMap.get("dbcode").toString();
			HandlerDataSource.setDataSource(dbcode);
			resultMap = meetCheckService.selectMeetCheckByPage(paramsMap);
			resultMap.put("pubKey",Optional.ofNullable(request.getAttribute("pubKey")).orElse(""));
		} catch (Exception e) {
			logger.info("selectMeetCheckByPage" + e.toString());
			resultMap.put("returnCode", 0);
			resultMap.put("returnMsg", "查询失败");
		} finally {
			HandlerDataSource.clear();
			return JSON.toJSONString(resultMap);
		}
		
	}
	
	/**
	 * 不分页查询主表所有记录
	 * 
	 * @param request
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/selectMeetCheckAll", method = { RequestMethod.GET,
			RequestMethod.POST }, produces = "application/json; charset=utf-8")
	public @ResponseBody String selectMeetCheckAll(HttpServletRequest request) throws UnsupportedEncodingException {
		request.setCharacterEncoding("UTF-8");
		Map<String,Object> resultMap = new HashMap<>();
		try {
			String info= request.getParameter("info");
			Map<String,Object> paramsMap = Tools.JsonToMap(info);
			String dbcode = paramsMap.get("dbcode").toString();
			HandlerDataSource.setDataSource(dbcode);
			resultMap = meetCheckService.selectMeetCheckAll(paramsMap);
			resultMap.put("pubKey",Optional.ofNullable(request.getAttribute("pubKey")).orElse(""));
		} catch (Exception e) {
			logger.info("selectMeetCheckAll" + e.toString());
			resultMap.put("returnCode", 0);
			resultMap.put("returnMsg", "查询失败");
		} finally {
			HandlerDataSource.clear();
			return JSON.toJSONString(resultMap);
		}
		
	}
	
	/**
	 * 分页查询子表
	 * 
	 * @param request
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/selectMeetCheckDetailByPage", method = { RequestMethod.GET,
			RequestMethod.POST }, produces = "application/json; charset=utf-8")
	public @ResponseBody String selectMeetCheckDetailByPage(HttpServletRequest request) throws UnsupportedEncodingException {
		request.setCharacterEncoding("UTF-8");
		Map<String,Object> resultMap = new HashMap<>();
		try {
			String info= request.getParameter("info");
			Map<String,Object> paramsMap = Tools.JsonToMap(info);
			String dbcode = paramsMap.get("dbcode").toString();
			HandlerDataSource.setDataSource(dbcode);
			resultMap = meetCheckService.selectMeetCheckDetailByPage(paramsMap);
			resultMap.put("pubKey",Optional.ofNullable(request.getAttribute("pubKey")).orElse(""));
		} catch (Exception e) {
			logger.info("selectMeetCheckDetailByPage" + e.toString());
			resultMap.put("returnCode", 0);
			resultMap.put("returnMsg", "查询失败");
		} finally {
			HandlerDataSource.clear();
			return JSON.toJSONString(resultMap);
		}	
	}
	
	/**
	 * 更新子表
	 * 
	 * @param request
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/updateMeetCheckDetail", method = { RequestMethod.GET,
			RequestMethod.POST }, produces = "application/json; charset=utf-8")
	public @ResponseBody String updateMeetCheckDetail(HttpServletRequest request) throws UnsupportedEncodingException {
		request.setCharacterEncoding("UTF-8");
		Map<String,Object> resultMap = new HashMap<>();
		try {
			String info= request.getParameter("info");
			Map<String,Object> paramsMap = Tools.JsonToMap(info);
			String dbcode = paramsMap.get("dbcode").toString();
			HandlerDataSource.setDataSource(dbcode);
			resultMap = meetCheckService.updateMeetCheckDetail(paramsMap);
			resultMap.put("pubKey",Optional.ofNullable(request.getAttribute("pubKey")).orElse(""));
		} catch (Exception e) {
			logger.info("updateMeetCheckDetail" + e.toString());
			resultMap.put("returnCode", 0);
			resultMap.put("returnMsg", "更新失败");
		} finally {
			HandlerDataSource.clear();
			return JSON.toJSONString(resultMap);
		}
		
	}
	
	@SuppressWarnings("finally")
	@RequestMapping(value = "/findMeetCheckCount", method = { RequestMethod.GET,
			RequestMethod.POST }, produces = "application/json; charset=utf-8")
	public @ResponseBody String findMeetCheckCount(HttpServletRequest request) throws UnsupportedEncodingException {
		request.setCharacterEncoding("UTF-8");
		Map<String,Object> resultMap = new HashMap<>();
		try {
			String info= request.getParameter("info");
			Map<String,Object> paramsMap = Tools.JsonToMap(info);
			String dbcode = paramsMap.get("dbcode").toString();
			HandlerDataSource.setDataSource(dbcode);
			resultMap = meetCheckService.findMeetCheckCountService(paramsMap);
			resultMap.put("pubKey",Optional.ofNullable(request.getAttribute("pubKey")).orElse(""));
		} catch (Exception e) {
			logger.info("findMeetCheckCount" + e.toString());
			resultMap.put("returnCode", 0);
			resultMap.put("returnMsg", "查询失败");
		} finally {
			HandlerDataSource.clear();
			return JSON.toJSONString(resultMap);
		}	
	}
	
	/**
	 * 批量更新子表
	 * 
	 * @param request
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/updateBatchMeetCheckDetail", method = { RequestMethod.GET,
			RequestMethod.POST }, produces = "application/json; charset=utf-8")
	public @ResponseBody String updateBatchMeetCheckDetail(HttpServletRequest request) throws UnsupportedEncodingException {
		request.setCharacterEncoding("UTF-8");
		Map<String,Object> resultMap = new HashMap<>();
		try {
			String info= request.getParameter("info");
			List<Map<String,Object>> dataList = new ArrayList<Map<String,Object>>();
			//将jsonArray字符串转化为JSONArray数组 
		    JSONArray jsonArray = JSONArray.fromObject(info); 
		    int jsonArraySize = jsonArray.size();//获取数组长度
			for(int i = 0; i < jsonArraySize; i++ ){
				//JSON对象转JSON字符串
				String str = jsonArray.getJSONObject(i).toString();
				Map<String,Object> paramsMap = Tools.JsonToMap(str);
				dataList.add(paramsMap);
			}
			String dbcode = dataList.get(0).get("dbcode").toString();
			HandlerDataSource.setDataSource(dbcode);
			resultMap = meetCheckService.updateBatchMeetCheckDetail(dataList);
			resultMap.put("pubKey",Optional.ofNullable(request.getAttribute("pubKey")).orElse(""));
		} catch (Exception e) {
			logger.info("updateBatchMeetCheckDetail" + e.toString());
			resultMap.put("returnCode", 0);
			resultMap.put("returnMsg", "更新失败");
		} finally {
			HandlerDataSource.clear();
			return JSON.toJSONString(resultMap);
		}
		
	}
}
